package com.maxt.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.maxt.base.entity.User;
import com.maxt.common.util.PropertyUtil;
import com.maxt.service.UserService;

@Controller
public class DemoController {
	
	@Autowired
	UserService userService;
	
	public static final Logger logger = LoggerFactory.getLogger(DemoController.class);
	
	//获取log4j.properties 日志记录器实例,实现日志定制功能
	public static final Logger loggerHit = LoggerFactory.getLogger("HIT");
	
	/**
	 * 返回jsp视图
	 * @return
	 */
	@RequestMapping("/jsp_view")
	public String index(){
		
		//返回WEB-INF/view/demo.jsp 视图
		return "demo";
	}
	
	/**
	 * 返回json数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/resp_json",method=RequestMethod.POST)
	@ResponseBody
	public Object responseTest(HttpServletRequest request){
		
		String name = "小明";
		String age = "23";
		
		Map<String, Object> rmMap = new HashMap<String, Object>();
		rmMap.put("name", name);
		rmMap.put("age", age);
		
		return rmMap;
	}
	
	/**
	 * user入库测试
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/user",method=RequestMethod.POST)
	@ResponseBody
	public Object userTest(HttpServletRequest request){
		
		User user = new User();
		user.setName("丽丽");
		user.setAge(22);
		user.setTel("13709876567");
		user.setSex("女");
		user.setAdd_time(new Date());
		
		userService.insertUser(user);
		
		return user;
	}
	
	/**
	 * config.properties读取测试
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/config",method=RequestMethod.POST)
	@ResponseBody
	public Object configTest(HttpServletRequest request){
		
		String numMin = PropertyUtil.getProperties().getProperty("numMin");
		String numMax = PropertyUtil.getProperties().getProperty("numMax");
		
		Map<String, String> rmMap = new HashMap<String, String>();
		rmMap.put("numMin", numMin);
		rmMap.put("numMax", numMax);
		
		return rmMap;
	}
	
	/**
	 * log4j测试
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/log",method=RequestMethod.POST)
	@ResponseBody
	public Object logTest(HttpServletRequest request){
		
		logger.info("this is log4j info-------------------------->time:"+System.currentTimeMillis());
		logger.error("this is log4j error-------------------------->time:"+System.currentTimeMillis());
		
		//日志定制
		loggerHit.info("this is loggerHit info-------------------->time:"+System.currentTimeMillis());
		loggerHit.error("this is loggerHit error-------------------->time:"+System.currentTimeMillis());
		
		return null;
	}
	
}
